package ch06.d2;

import org.junit.Test;

public class Car {
    int x;
    int y;

    // N1 E2 S3 W4
    String[] directions={"N","E","S","W"};
    int directionIndex;//方向 0 1 2 3

    public Car(int x,int y,String direction){
        // 初始化数据
        this.x=x;
        this.y=y;
        //到数组中找
        for (int i = 0; i <directions.length; i++) {
            String s=directions[i];
            if(s.equals(direction)){
                this.directionIndex=i;
            }
        }
    }

    public void move(String comment){
        // 拆分指令
        char[] cs=comment.toCharArray();

        for (char c : cs) {
            switch (c){
                case 'L':
                    turnLeft();
                    break;
                case 'R':
                    turnRight();
                    break;
                case 'M':
                    move();
                    break;
            }
        }
        StringBuffer bud;
    }

    private void turnRight() {
        this.directionIndex++;
        if(this.directionIndex==4)
            this.directionIndex=0;
    }

    private void turnLeft() {
        this.directionIndex--;
        if(this.directionIndex==-1)
            this.directionIndex=3;
    }

    private void move(){
        switch (this.directionIndex){
            case 0://北
                this.y++;
                break;
            case 1:
                this.x++;
                break;
            case 2://南
                this.y--;
                break;
            case 3:
                this.x--;
                break;
        }



    }

    @Override
    public String toString() {
        return "Car{" +
                "x=" + x +
                ", y=" + y +
                ", direction=" + directions[directionIndex] +
                '}';
    }

    public Car(){

    }

    @Test
    public void testCar(){

    }
}
